/*******************************************************************************
 * Copyright (c) 2010 Torkild U. Resheim
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Torkild U. Resheim - initial API and implementation
 *******************************************************************************/
package no.resheim.buildmonitor.ui.properties;

import no.resheim.buildmonitor.core.data.Server;

import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;

public class ServerPropertySource implements IPropertySource {

	private static final String POLL_INTERVAL = "pollInterval";
	private static final String DESCRIPTION = "description";
	private static final String STATUS = "status";
	private static final String URI = "uri";
	private static final String NAME = "name";
	private final Server server;

	public ServerPropertySource(Server server) {
		this.server = server;
	}

	@Override
	public Object getEditableValue() {
		return this;
	}

	@Override
	public IPropertyDescriptor[] getPropertyDescriptors() {
		return new IPropertyDescriptor[] {
				new TextPropertyDescriptor(POLL_INTERVAL, "Poll Interval"),
				new TextPropertyDescriptor(NAME, "Name"),
				new PropertyDescriptor(URI, "URI"),
				new PropertyDescriptor(DESCRIPTION, "Description"),
				new PropertyDescriptor(STATUS, "Status") };
	}

	@Override
	public Object getPropertyValue(Object id) {
		if (id.equals(NAME))
			return server.getName();
		if (id.equals(URI))
			return server.getUri();
		if (id.equals(STATUS))
			return server.getStatus();
		if (id.equals(DESCRIPTION))
			return server.getDescription();
		if (id.equals(POLL_INTERVAL))
			return Integer.toString(server.getPollInterval());
		return null;
	}

	@Override
	public boolean isPropertySet(Object id) {
		return false;
	}

	@Override
	public void resetPropertyValue(Object id) {
	}

	@Override
	public void setPropertyValue(Object id, Object value) {
		if (id.equals(POLL_INTERVAL)) {
			server.setPollInterval(Integer.parseInt(value.toString()));
		}
		if (id.equals(NAME)) {
			server.setName(value.toString());
		}
	}

}
